﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IntelligentDecisioning.SharePoint.Controls;
using Microsoft.SharePoint;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebPartPages;

namespace IntelligentDecisioning.SharePoint.Actions
{
    /// <summary>
    /// Performs first load provisioning on one or more sharepoint pages.
    /// 
    /// We can either run the proviosing actions on all the pages in the target
    /// site or a selected subset by specifying their pagenames explicitly.
    /// </summary>
    public class ProvisionPageAction: ActionBase
    {
        #region Properties

        /// <summary>
        /// Set to true to run the provisioning actions
        /// on all aspx pages in the sharepoint site
        /// </summary>
        public bool AllPages { get; set; }

        /// <summary>
        /// Specify a list of page names in the current site that
        /// we want to run our provisioning actions on.
        /// </summary>
        public List<String> PageNames { get; set; }

        /// <summary>
        /// The list of provisioning actions that we want to 
        /// run on our target pages
        /// </summary>
        public List<IPageAction> Actions { get; set; }

        #endregion

        #region Constructor

        public ProvisionPageAction()
        {
            PageNames = new List<String>();
            Actions = new List<IPageAction>();
        }

        #endregion

        #region Provision

        public override void Provision(IDProvisioningControlBase context)
        {
         
            // Get the pages
            List<SPFile> pages = GetPages(context.CurrentWeb);

            foreach (SPFile page in pages)
            {
                
                // Apply the actions
                foreach (IPageAction pageAction in Actions)
                {
                    SPLimitedWebPartManager wpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared); 
                    pageAction.Provision(page, context.CurrentWeb, context.CurrentSite, wpm, context.Log);
                    page.Update();
                    context.CurrentWeb.Update();
                    wpm.Dispose();
                }
            }
        }

        #endregion


        #region Get Pages


        private List<SPFile> GetPages(SPWeb currentWeb)
        {
            List<SPFile> pages = new List<SPFile>();

            if(AllPages)
            {
                //get all the top level aspx files
                // TODO:recursion
                foreach (SPFile file in currentWeb.RootFolder.Files)
                {
                    if(file.Name.EndsWith(".aspx"))
                    {
                        pages.Add(file);
                    }
                }
            }
            else
            {
                foreach (SPFile file in currentWeb.RootFolder.Files)
                {
                    if (PageNames.Contains(file.Name.ToLower()))
                    {
                        pages.Add(file);
                    }
                }
            }

            return pages;
        }

        #endregion
    }
}
